package com.yanqu.road.server.gameplayer.module.activity.eggs;

import com.yanqu.road.entity.activity.eggs.*;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.logic.bussiness.activity.EggsActivityBussiness;
import com.yanqu.road.pb.eggs.EggsActivityProto;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.base.GeneralModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.manger.activity.eggs.EggsActivityConfig;
import com.yanqu.road.server.manger.activity.eggs.EggsActivityMgr;
import com.yanqu.road.server.pb.EggsActivityPb;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.util.Objects;

public class EggsActivityModule extends GeneralModule {
    private EggsUserData eggsUserData;
    public EggsActivityModule(GamePlayer player) {
        super(player);
    }

    @Override
    public boolean loadData() {
        if(EggsActivityMgr.isActivityInShowTime()) {
            EggsActivityConfig eggsActivityConfig = EggsActivityMgr.getEggsActivityConfig();
            if(null != eggsActivityConfig) {
                eggsUserData = EggsActivityBussiness.getEggsUserData(eggsActivityConfig.getActivityId(), player.getUserId());
            }
        }
        return true;
    }

    @Override
    public boolean afterLoadData() {
        return true;
    }

    @Override
    public boolean saveData() {
        if(null != eggsUserData){
            if(eggsUserData.isInsertOption()){
                EggsActivityBussiness.addEggsUserData(eggsUserData);
            }else if(eggsUserData.isUpdateOption()){
                EggsActivityBussiness.updateEggsUserData(eggsUserData);
            }
        }
        return true;
    }

    @Override
    public void afterLogin() {
        syncActivityConfig();
        initUserData();
        syncUserData();
    }

    /**
    * 描述：同步活动配置
    * 作者：zrq
    * 时间：2022/11/23
    * 参数：

    * 返回值：
    **/
    public void syncActivityConfig(){
        if(EggsActivityMgr.isActivityInShowTime()){
            EggsActivityConfig eggsActivityConfig = EggsActivityMgr.getEggsActivityConfig();
            if(null == eggsActivityConfig){
                return;
            }
            EggsActivityProto.EggsSyncConfigMsg.Builder syncMsg = EggsActivityPb.parseEggsSyncConfigMsg(eggsActivityConfig, player.getLanguage());
            if(null != syncMsg){
                player.sendPacket(Protocol.U_EGGS_ACTIVITY_CONFIG_SYNC, syncMsg);
            }
        }
    }

    /**
    * 描述：同步玩家数据
    * 作者：zrq
    * 时间：2022/11/23
    * 参数：

    * 返回值：
    **/
    public void syncUserData(){
        if(EggsActivityMgr.isActivityInShowTime()){
            if(null == eggsUserData){
                return;
            }
            EggsActivityProto.EggsUserDataMsg.Builder dataMsg = EggsActivityPb.parseEggsUserDataMsg(eggsUserData);
            if(null != dataMsg){
                player.sendPacket(Protocol.U_EGGS_USER_DATA_SYNC, dataMsg);
            }
        }
    }

    /**
    * 描述：初始化玩家数据
    * 作者：zrq
    * 时间：2022/11/24
    * 参数：

    * 返回值：
    **/
    public void initUserData(){
        if(EggsActivityMgr.isActivityInShowTime()){
            EggsActivityConfig eggsActivityConfig = EggsActivityMgr.getEggsActivityConfig();
            if(null != eggsActivityConfig){
                initUserData(eggsActivityConfig.getActivityId());
            }
        }
    }
    public synchronized void initUserData(int activityId){
        if(null == eggsUserData || eggsUserData.getActivityId() != activityId){
            EggsActivityConfig eggsActivityConfig = EggsActivityMgr.getEggsActivityConfig();
            if(null != eggsActivityConfig){
                EggsUserData tempUserData = new EggsUserData();
                tempUserData.setActivityId(activityId);
                tempUserData.setUserId(getUserId());
                tempUserData.setInsertOption();
                eggsUserData = tempUserData;
            }
        }
    }

    /**
    * 描述：课堂回答
    * 作者：zrq
    * 时间：2022/11/23
    * 参数：

    * 返回值：
    **/
    public int classRoomAnswer(int activityId, int id, int choose){
        if(1 != choose && 2 != choose){
            return GameErrorCode.E_EGGS_ACTIVITY_CLASS_ROOM_WRONG_ANSWER;
        }
        if(!EggsActivityMgr.isActivityInShowTime(activityId)){
            return GameErrorCode.E_ACTIVITY_NO_FOUND;
        }
        if(null == eggsUserData || eggsUserData.getActivityId() != activityId){
            return GameErrorCode.E_ACTIVITY_NO_FOUND;
        }
        if(eggsUserData.getClassRoomIsReward(id)){
            return GameErrorCode.E_EGGS_ACTIVITY_REWARD_IS_GET;
        }
        EggsActivityConfig eggsActivityConfig = EggsActivityMgr.getEggsActivityConfig();
        if(null == eggsActivityConfig){
            return GameErrorCode.E_ACTIVITY_NO_FOUND;
        }
        EggsClassRoomConfig eggsClassRoomConfig = eggsActivityConfig.getClassRoomConfig(id);
        if(null == eggsClassRoomConfig){
            return GameErrorCode.E_EGGS_ACTIVITY_CLASS_ROOM_NO_EXIST;
        }
        if(StringUtils.isNullOrEmpty(eggsClassRoomConfig.getTest())){
            return GameErrorCode.E_EGGS_ACTIVITY_CLASS_ROOM_CONFIG_ERROR;
        }
        String[] paramArr = eggsClassRoomConfig.getTest().split("\\|");
        if(paramArr.length != 3){
            return GameErrorCode.E_EGGS_ACTIVITY_CLASS_ROOM_CONFIG_ERROR;
        }
        String testStr = paramArr[choose];
        if(StringUtils.isNullOrEmpty(testStr)){
            return GameErrorCode.E_EGGS_ACTIVITY_CLASS_ROOM_CONFIG_ERROR;
        }
        String[] testArr = testStr.split(";");
        if(testArr.length != 2){
            return GameErrorCode.E_EGGS_ACTIVITY_CLASS_ROOM_CONFIG_ERROR;
        }
        if(!Objects.equals("1", testArr[0])){
            return GameErrorCode.E_EGGS_ACTIVITY_CLASS_ROOM_WRONG_ANSWER;
        }
        //标识领奖
        eggsUserData.setClassRoomReward(id);
        //加奖励
        Property property = PropertyHelper.parseStringToProperty(eggsClassRoomConfig.getRewards());
        player.getModule(CurrencyModule.class).addCurrency(property, eLogMoneyType.Activity, eLogMoneyType.EggsActivityClassRoomAnswer);
        syncUserData();
        EggsActivityProto.EggsClassRoomRespMsg.Builder respMsg = EggsActivityProto.EggsClassRoomRespMsg.newBuilder();
        respMsg.setRet(0);
        respMsg.setReward(eggsClassRoomConfig.getRewards());
        player.sendPacket(Protocol.U_EGGS_CLASS_ROOM_ANWSER, respMsg);
        return 0;
    }

    /**
    * 描述：问答题回答
    * 作者：zrq
    * 时间：2022/11/23
    * 参数：

    * 返回值：
    **/
    public int testAnswer(int activityId, int choose){
        if(1 != choose && 2 != choose){
            return GameErrorCode.E_EGGS_ACTIVITY_TEST_ANSWER_ERROR;
        }
        if(!EggsActivityMgr.isActivityInShowTime(activityId)){
            return GameErrorCode.E_ACTIVITY_NO_FOUND;
        }
        if(null == eggsUserData || eggsUserData.getActivityId() != activityId){
            return GameErrorCode.E_ACTIVITY_NO_FOUND;
        }
        EggsActivityConfig eggsActivityConfig = EggsActivityMgr.getEggsActivityConfig();
        if(null == eggsActivityConfig){
            return GameErrorCode.E_ACTIVITY_NO_FOUND;
        }
        int testId = eggsUserData.getCurTestId() + 1;
        if(testId > eggsActivityConfig.getMaxTestId()){
            return GameErrorCode.E_EGGS_ACTIVITY_TEST_ALL_ANSWER;
        }
        EggsTestConfig eggsTestConfig = eggsActivityConfig.getTestConfig(testId);
        if(null == eggsTestConfig){
            return GameErrorCode.E_EGGS_ACTIVITY_TEST_NO_EXIST;
        }
        if(StringUtils.isNullOrEmpty(eggsTestConfig.getDesc())){
            return GameErrorCode.E_EGGS_ACTIVITY_TEST_CONFIG_ERROR;
        }
        String[] paramArr = eggsTestConfig.getDesc().split("\\|");
        if(paramArr.length != 3){
            return GameErrorCode.E_EGGS_ACTIVITY_TEST_CONFIG_ERROR;
        }
        String testStr = paramArr[choose];
        if(StringUtils.isNullOrEmpty(testStr)){
            return GameErrorCode.E_EGGS_ACTIVITY_TEST_CONFIG_ERROR;
        }
        String[] testArr = testStr.split(";");
        if(testArr.length != 2){
            return GameErrorCode.E_EGGS_ACTIVITY_TEST_CONFIG_ERROR;
        }
        //设置选择
        eggsUserData.setTestChoose(testId, choose);
        eggsUserData.setCurTestId(testId);
        //最后一题，结算
        EggsActivityProto.EggsClassRoomRespMsg.Builder respMsg = EggsActivityProto.EggsClassRoomRespMsg.newBuilder();
        respMsg.setRet(0);
        if(testId == eggsActivityConfig.getMaxTestId() && !eggsUserData.isTestReward()){
            int totalScore = 0;
            if(null != eggsUserData.getTestDataMap()){
                for(EggsTestData data : eggsUserData.getTestDataMap().values()){
                    if(data.getId() > eggsActivityConfig.getMaxTestId()){
                        continue;
                    }
                    eggsTestConfig = eggsActivityConfig.getTestConfig(data.getId());
                    if(null == eggsTestConfig){
                        continue;
                    }
                    if(StringUtils.isNullOrEmpty(eggsTestConfig.getDesc())){
                        continue;
                    }
                    paramArr = eggsTestConfig.getDesc().split("\\|");
                    if(paramArr.length != 3){
                        continue;
                    }
                    testStr = paramArr[data.getChoose()];
                    if(StringUtils.isNullOrEmpty(testStr)){
                        continue;
                    }
                    testArr = testStr.split(";");
                    if(testArr.length != 2){
                        continue;
                    }
                    int score = StringUtils.string2intSafe(testArr[0], 0);
                    totalScore += score;
                }
            }
            String reward = eggsActivityConfig.getTestReward(totalScore);
            Property property = PropertyHelper.parseStringToProperty(reward);
            eggsUserData.setTestReward(true);
            player.getModule(CurrencyModule.class).addCurrency(property, eLogMoneyType.Activity, eLogMoneyType.EggsActivityTestReward);
            respMsg.setReward(reward);
        }
        syncUserData();
        player.sendPacket(Protocol.U_EGGS_TEST_ANWSER, respMsg);
        return 0;
    }

    /**
    * 描述：听曲
    * 作者：zrq
    * 时间：2022/11/23
    * 参数：

    * 返回值：
    **/
    public int songListen(int activityId, int id){
        if(!EggsActivityMgr.isActivityInShowTime(activityId)){
            return GameErrorCode.E_ACTIVITY_NO_FOUND;
        }
        if(null == eggsUserData || eggsUserData.getActivityId() != activityId){
            return GameErrorCode.E_ACTIVITY_NO_FOUND;
        }
        if(eggsUserData.getSongIsReward(id)){
            return GameErrorCode.E_EGGS_ACTIVITY_REWARD_IS_GET;
        }
        EggsActivityConfig eggsActivityConfig = EggsActivityMgr.getEggsActivityConfig();
        if(null == eggsActivityConfig){
            return GameErrorCode.E_ACTIVITY_NO_FOUND;
        }
        EggsSongConfig eggsSongConfig = eggsActivityConfig.getSongConfig(id);
        if(null == eggsSongConfig){
            return GameErrorCode.E_EGGS_ACTIVITY_SONG_NO_EXIST;
        }
        //标识领奖
        eggsUserData.setSongReward(id);
        //加奖励
        Property property = PropertyHelper.parseStringToProperty(eggsSongConfig.getRewards());
        player.getModule(CurrencyModule.class).addCurrency(property, eLogMoneyType.Activity, eLogMoneyType.EggsActivitySongReward);
        syncUserData();
        EggsActivityProto.EggsSongRespMsg.Builder respMsg = EggsActivityProto.EggsSongRespMsg.newBuilder();
        respMsg.setRet(0);
        respMsg.setReward(eggsSongConfig.getRewards());
        player.sendPacket(Protocol.U_EGGS_SONG_LISTEN, respMsg);
        return 0;
    }

    /**
    * 描述：听故事
    * 作者：zrq
    * 时间：2022/11/23
    * 参数：

    * 返回值：
    **/
    public int storyListen(int activityId, int id){
        if(!EggsActivityMgr.isActivityInShowTime(activityId)){
            return GameErrorCode.E_ACTIVITY_NO_FOUND;
        }
        if(null == eggsUserData || eggsUserData.getActivityId() != activityId){
            return GameErrorCode.E_ACTIVITY_NO_FOUND;
        }
        if(eggsUserData.getStoryIsReward(id)){
            return GameErrorCode.E_EGGS_ACTIVITY_REWARD_IS_GET;
        }
        EggsActivityConfig eggsActivityConfig = EggsActivityMgr.getEggsActivityConfig();
        if(null == eggsActivityConfig){
            return GameErrorCode.E_ACTIVITY_NO_FOUND;
        }
        EggsStoryConfig eggsStoryConfig = eggsActivityConfig.getStoryConfig(id);
        if(null == eggsStoryConfig){
            return GameErrorCode.E_EGGS_ACTIVITY_STORY_NO_EXIST;
        }
        //标识领奖
        eggsUserData.setStoryReward(id);
        //加奖励
        Property property = PropertyHelper.parseStringToProperty(eggsStoryConfig.getRewards());
        player.getModule(CurrencyModule.class).addCurrency(property, eLogMoneyType.Activity, eLogMoneyType.EggsActivityStoryReward);
        syncUserData();
        EggsActivityProto.EggsStoryRespMsg.Builder respMsg = EggsActivityProto.EggsStoryRespMsg.newBuilder();
        respMsg.setRet(0);
        respMsg.setReward(eggsStoryConfig.getRewards());
        player.sendPacket(Protocol.U_EGGS_STORY_LISTEN, respMsg);
        return 0;
    }

    public EggsUserData getEggsUserData() {
        return eggsUserData;
    }
}
